package com.example.quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/2/14 23:27
 */
public class QuartzMain {

    public static Scheduler createScheduler() throws
            SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        return schedulerFactory.getScheduler();
    }

    public static JobDetail createJob() {
        JobBuilder jobBuilder = JobBuilder.newJob(Job.class);
        jobBuilder.withIdentity("jobName1", "myJob1");
        return jobBuilder.build();
    }

    public static Trigger createTrigger(String cycleTime) {
        return TriggerBuilder.newTrigger()
                .withIdentity("triggerName1", "myTrigger1")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(cycleTime))
                .build();
    }

    public static void start(String cycleTime) throws SchedulerException {
        Scheduler scheduler = QuartzMain.createScheduler();
        JobDetail job = QuartzMain.createJob();
        Trigger trigger = QuartzMain.createTrigger(cycleTime);
        scheduler.scheduleJob(job, trigger);
        scheduler.start();
    }

}
